import { _decorator, Component, Node, Prefab, Animation, instantiate, Label } from 'cc';
import { GameData } from './GameData';
import { gameEvent, GameEventType, GameTimer } from './Global';
import { Code } from './Code';
const { ccclass, property } = _decorator;

// 没有用到了
@ccclass('Answer')
export class Answer extends Component {

    @property({ type: Prefab })
    CodePrefab: Prefab = null;

    start() {

    }
    protected onLoad(): void {
        gameEvent.on(GameEventType.CODE_CLICK, this.onCodeCheck, this);
    }

    onEnable() {
        // gameEvent.on('clickCode', this.onCodeCheck, this);
    }

    onDisable() {
        gameEvent.off(GameEventType.CODE_CLICK, this.onCodeCheck, this);
    }

    update(deltaTime: number) {

    }

    onCodeCheck(char: string) {
        if (GameData.isSuccess()) {
            console.log('恭喜你答对了！');

        } else {

            GameData.answerWords += char;

            const codeNode = instantiate(this.CodePrefab);
            const label = codeNode.getChildByName('Label').getComponent(Label)
            label.string = char;
            this.node.addChild(codeNode);
            // 不可点击
            codeNode.getComponent(Code).status = 'disable';
            // 播放动画
            const animation = codeNode.getComponent(Animation);
            animation.play('code_add_anim');

            if (GameData.isSuccess()) {
                // 成功通关
                GameTimer.pause();
                gameEvent.emit(GameEventType.GAME_SUCCESS, GameEventType.GAME_SUCCESS);
            }
        }
    }


}